// 使用网上接口
const domain = "http://www.chaizhicheng.top:3000";

const sessionIdKey = "sessionToken";

// post方法，使用Promise实现，返回Promise对象
function post(link, req) {
  // 获取localStorage中的用户token
  var sessionId = wx.getStorageSync("sessionId");
  // 拼接完整url
  var url = domain + link;

  var promise = new Promise(function (resolve, reject) {
    wx.showLoading({
      icon: 'none',
      title: '加载中...',
    }),
      wx.request({
        url: url,
        data: req,
        method: "POST",
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'sessionId': sessionId,
          'Cookie': 'sessionId=' + sessionId
        },
        success: resolve,
        fail: reject,
        complete: wx.hideLoading(),
      });
  });
  return promise;
}

// 将获取的session设置到localstorage
function setSession(value) {
  wx.setStorage({
    key: sessionIdKey,
    data: value,
  })
}

// get方法，Promise实现
const request = (url, data) => {
  let _url = domain + url;
  return new Promise((resolve, reject) => {
    wx.request({
      url: _url,
      method: "get",
      data: data,
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success(request) {
        resolve(request.data)
      },
      fail(error) {
        reject(error)
      },
      complete: wx.hideLoading(),
    })
  });
}
/*
 * 对外暴露接口
 */
module.exports = {
  request,
  post,
  setSession,
  domain,
  gethotsongs: (data) => {
    return request('/search/hot', data)//热搜接口
  },
  searchSuggest: (data) => {
    return request('/search/suggest', data)//搜索建议接口
  },
  searchResult: (data) => {
    return request('/search', data)//搜索结果接口
  },
  getBanner: (data) => {
    return request('/banner', data)//个性推荐轮播
  },
  getsongsheet: (data) => {
    return request('/top/playlist', data)//热门歌单接口
  },
  getNewSong: (data) => {
    return request('/personalized/newsong', data)//最新音乐接口
  },
  getDjRadios: (data) => {
    return request('/dj/recommend', data)//电台推荐接口
  },
  getProgramRecommend: (data) => {
    return request('/program/recommend', data)//推荐节目接口
  },
  getRecommendType: (data) => {
    return request('/dj/recommend/type', data)//所有电台分类推荐
  },
  getRecommendMV: (data) => {
    return request('/personalized/mv', data)//推荐MV
  },
  getNewMv: (data) => {
    return request('/mv/first', data)//最新MV
  },
  getNewEst: (data) => {
    return request('/album/newest', data)//最新专辑
  },
  getTopList: (data) => {
    return request('/top/list', data)//排行榜
  },
  getDjList: (data) => {
    return request('/dj/catelist', data) //电台分类
  },
  getPay: (data) => {
    return request('/dj/paygift', data)//付费精品
  },
  getSonger: (data) => {
    return request('/toplist/artist', data)//歌手排行
  }
}